
package com.yimixian.app.order;

import com.yimixian.app.R;
import com.yimixian.app.model.ValidatedOrder;
import com.yimixian.app.util.CustomViewUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class DeliveryModeItemView extends FrameLayout {

    @InjectView(R.id.delivery_text)
    TextView mDeliveryText;
    @InjectView(R.id.check_image)
    ImageView mCheckImage;

    private ValidatedOrder.DeliveryOptions mDeliveryMode;
    private boolean mChecked = false;

    public DeliveryModeItemView(Context context) {
        super(context);
        initDeliveryModeView();
    }

    public DeliveryModeItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDeliveryModeView();
    }

    public DeliveryModeItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDeliveryModeView();
    }

    private void initDeliveryModeView() {
        LayoutInflater.from(getContext()).inflate(R.layout.delivery_mode_item_view, this);
        ButterKnife.inject(this);
        CustomViewUtils.setViewBackground(this,
                getResources().getDrawable(R.drawable.settings_item_bg));
    }

    public void bind(ValidatedOrder.DeliveryOptions deliveryMode) {
        mDeliveryMode = deliveryMode;
        mDeliveryText.setText(deliveryMode.getName());
    }

    public int getDeliveryModeId() {
        return mDeliveryMode.getId();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (!mChecked) {
            setCheckImageVisibility(View.GONE);
        } else {
            setCheckImageVisibility(View.VISIBLE);
        }
    }

    public void setCheckImageVisibility(int visibility) {
        mCheckImage.setVisibility(visibility);
    }

    public void setChecked(boolean checked) {
        mChecked = checked;
        mCheckImage.setImageResource(checked ? R.drawable.confirm_order_checked_image :
                R.drawable.confirm_order_unchecked_image);
    }
}
